<template>
  <div style="width: 600px; margin: 0 auto">
    <el-form>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          size="large"
          auto-complete="off"
          placeholder="账号"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          size="large"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter="handleLogin"
        >
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width: 100%"
          @click.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { login } from '@/api/login'
import { setToken } from '@/utils/token'
import router from '@/router'

const loginForm = ref({
  username: '',
  password: '',
})
const loading = ref(false)
const handleLogin = () => {
  login(loginForm.value).then((rsp) => {
    setToken(rsp.token)
    router.push({ name: 'GovList' })
  })
}
</script>
